<?php 
include 'method/win.php';

if(!isset($_SESSION['user'])){
    show_msg("请先登录!","login.php");
}
if(isset($_GET['action'])){
    logout();
}
if(isset($_POST['upload'])){
    if (isset($_FILES['imgfile']) && is_uploaded_file($_FILES['imgfile']['tmp_name'])){
        $imgFile = $_FILES['imgfile'];
        $upErr = $imgFile['error'];
        if ($upErr == 0){
            $imgType = $imgFile['type']; //文件类型。
            /* 判断文件类型，这个例子里仅支持jpg和gif类型的图片文件。*/
            if ($imgType == 'image/jpeg'|| $imgType == 'image/gif'){
                
                //通过反转及分割获取图片的后缀
                $string = strrev($imgFile['name']);
                $array = explode('.',$string);
                $ex = strrev($array[0]);
                
                //将上传图片重命名为日期加100-999的随机数并加上后缀名
                $imgFile['name'] = date("Y").date("m").date("d").date("H").date("i").date("s").rand(100, 999).".".$ex;
                
                $imgFileName = $imgFile['name'];
                $imgSize = $imgFile['size'];
                $imgTmpFile = $imgFile['tmp_name'];
                /* 将文件从临时文件夹移到上传文件夹中。*/
                move_uploaded_file($imgTmpFile, '../img/'.$imgFileName);
                /*显示上传后的文件的信息。*/
                //                 $strPrompt = sprintf("文件%s上传成功<br>". "文件大小: %s字节<br>". "<img src='upfile/%s'>", $imgFileName, $imgSize, $imgFileName);
                //                 echo $strPrompt;
                
                $imgPath="img/".$imgFile['name'];
                $sql = "update weibo_user set user_tx='".$imgPath."' where user_name='".$_SESSION['user']."'";
                $res = $conn->query($sql);
                if($res){
                    show_msg("修改成功", "index_loginafter.php");
                }else {
                    show_msg("修改失败", "fixtx.php");
                }
                
            }else{
                echo "请选择jpg或gif文件，不支持其它类型的文件。";
            }
        }else{
            echo "文件上传失败。<br>";
            switch ($upErr){
                case 1:
                    echo "超过了php.ini中设置的上传文件大小。";
                    break;
                case 2:
                    echo "超过了MAX_FILE_SIZE选项指定的文件大小。";
                    break;
                case 3:
                    echo "文件只有部分被上传。";
                    break;
                case 4:
                    echo "文件未被上传。";
                    break;
                case 5:
                    echo "上传文件大小为0";
                    break;
            }
        }
    }else{
        show_msg("无上传文件属于无效操作哦！", "fixtx.php");
    }
}

$sql="select * from weibo_user where user_name='".$_SESSION['user']."'";
$res=$conn->query($sql);
$row=$res->fetch_assoc();
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>修改头像</title>
    <link rel="stylesheet" href="../lib/bootstrap/css/bootstrap.css">
    <link rel="stylesheet" href="css/base.css">
    <link rel="stylesheet" href="css/fixtx.css">
    <script src="../lib/jquery/jquery.js" type="text/javascript"></script>
    <script src="../lib/bootstrap/js/bootstrap.js" type="text/javascript"></script>
</head>
<body>
<!--顶部导航条-->
<nav class="navbar navbar-default">
    <div class="container-fluid">
        <!--头部logo-->
        <div class="navbar-header">
            <a class="navbar-brand" href="#"><em><img src="../img/logo.jpg" height=30 width=30></em><em>小狗微博</em></a>
        </div>

        <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
            <!--搜索框-->
            <form class="navbar-form navbar-left" role="search" method="post" action="search_loginafter.php">
                <div class="form-group">
                    <input type="text" class="form-control" style="width: 400px" placeholder="Search" name="str-search">
                </div>
                <button type="sumbit" class="btn btn-default" name="search-btn">搜索</button>
            </form>
            <ul class="nav navbar-nav navbar-right">
                <li><a href="index_loginafter.php">首页</a></li>
                <li><a href="person_loginafter.php"><?php echo $_SESSION['user']?></a></li>
                <li class="dropdown">
                    <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">我的消息 <span class="caret"></span></a>
                    <ul class="dropdown-menu">
                        <li><a href="inbox.php?page=1">@我</a></li>
                        <li><a href="inbox.php?page=2">评论</a></li>
                        <li><a href="inbox.php?page=3">回复</a></li>
                        <li><a href="inbox.php?page=4">赞</a></li>
                    </ul>
                </li>
                <li class="dropdown">
                    <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">个人中心 <span class="caret"></span></a>
                    <ul class="dropdown-menu">
                    	<li><a href="fixpw.php">修改密码</a></li>
                        <li><a href="fixtx.php">修改头像</a></li>
                        <li><a href="index_loginafter.php?action=logout">退出登录</a></li>
                    </ul>
                </li>
            </ul>
        </div>
    </div>
</nav>

<div class="fixtx-box">
    <label>修改头像</label>
    <div class="tx-box">
        <img src="../<?php echo $row['user_tx']?>" style="width: 100px; height: 100px">
    </div>

    <form action="" method="post" enctype="multipart/form-data" name="upload_form" id="upload_form" style="margin-top: 80px">
        <label>选择图片文件</label>
        <input name="imgfile" type="file" accept="image/gif, image/jpeg" style="width: 500px; display: block"/>
        <input name="upload" type="submit" value="上传" style="display: block; float: right; width: 100px"/>
    </form>
</div>
</body>
</html>